﻿using System.Collections.Generic;
using System.IdentityModel.Tokens;
using System.Linq;
using Microsoft.IdentityModel.Claims;
using Veracruz.Facebook;
using Veracruz.Facebook.IdentityModel;
using Veracruz.IdentityModel;

namespace Veracruz.ServiceModel.Facebook
{
    public class SocialNetworkService : ISocialNetworkService
    {
       #region fields
        #endregion
        #region ctors

        public SocialNetworkService()
        {

        }

        #endregion
        #region methods

        public string[] GetFriends(string uid)
        {
            // Get the caller's identity.
            IClaimsIdentity identity = (IClaimsIdentity)ClaimsPrincipal.Current.Identity;

            if (!identity.Claims.Exists(delegate(Claim claim) { return claim.ClaimType == IdentityClaimDefinitions.AuthenticationTokenClaim; }))
            {
                throw new SecurityTokenException("AuthenticationTokenClaim not found");
            }
            Claim l_authenticationTokenClaim = identity.Claims.Single<Claim>(claim => claim.ClaimType == IdentityClaimDefinitions.AuthenticationTokenClaim);
            Claim l_sessionKeyClaim = identity.Claims.Single<Claim>(claim => claim.ClaimType == IdentityClaimDefinitions.SessionKeyClaim);
            Claim l_uniqueIdentifierClaim = identity.Claims.Single<Claim>(claim => claim.ClaimType == IdentityClaimDefinitions.UniqueIdentifierClaim);

            UserInfo[] l_userInfos = FacebookHelper.GetFriends(l_sessionKeyClaim.Value, l_uniqueIdentifierClaim.Value);

            List<string> l_nameList = new List<string>();
            for (int i = 0; i < l_userInfos.Length; i++)
            {
                l_nameList.Add(l_userInfos[i].Name);
            }
            return l_nameList.ToArray();
        }

        //public void SetEmail(string email)
        //{
        //    // Get the caller's identity.
        //    IClaimsIdentity l_identity = (IClaimsIdentity)ClaimsPrincipal.Current.Identity;
        //    Session l_session = l_identity.GetSession();

        //    FacebookHelper.SetEmail(l_session, email);
        //}

        public string GetEmail()
        {
            // Get the caller's identity.
            IClaimsIdentity l_identity = (IClaimsIdentity)ClaimsPrincipal.Current.Identity;
            Session l_session = l_identity.GetSession();

            return FacebookHelper.GetEmail(l_session);
        }

        //public void SetUserInfo(string name, string email)
        //{
        //    //// Get the caller's identity.
        //    IClaimsIdentity l_identity = (IClaimsIdentity)ClaimsPrincipal.Current.Identity;
        //    Session l_session = l_identity.GetSession();

        //    //FacebookHelper.SetUserInfo(l_session, name, email);
        //}

        //public string GetUserInfo()
        //{
        //    // Get the caller's identity.
        //    IClaimsIdentity l_identity = (IClaimsIdentity)ClaimsPrincipal.Current.Identity;
        //    Session l_session = l_identity.GetSession();

        //    return FacebookHelper.GetLong(l_session);
        //}

        #endregion
    }
}
